﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using StedySoft.SenseSDK;

namespace Transportr
{
    public partial class SettingsForm : Form
    {
        #region Declarations
//        private bool _processEntireButton = false;
        private bool _buttonAnimation = true;
        private int _itmCounter = -1;
        private SenseListControl listSettings;
        #endregion

        public SettingsForm()
        {
            InitializeComponent();
            listSettings = new SenseListControl()
            {
                Dock = System.Windows.Forms.DockStyle.Fill,
                FocusedItem = null,
                IsSecondaryScrollType = false,
                MinimumMovement = 15,
                Name = "Settings",
                ShowScrollIndicator = true,
                Height = this.Height / 2,
                Springback = 0.35F,
                TabIndex = 1,
                ThreadSleep = 100,
                TopIndex = 0,
                Velocity = 0.9F
            };

            listSettings.BeginUpdate();

            foreach (Settings.MetaGroup group in Global.Settings.Store.metaData)
            {
                this.listSettings.AddItem(new StedySoft.SenseSDK.SensePanelDividerItem(
                       "DividerItem" + (this._itmCounter++).ToString("0#"), group.groupName));
                foreach (Settings.MetaItem item in group.items)
                {
                    switch (item.metaType)
                    {
                        case Settings.MetaType.Numeric:
                            SensePanelNumericItem itmNumeric = new SensePanelNumericItem()
                            {
                                Name = item.name,
                                AutoShowDialog = true,
                                AutoShowDialogText = item.description[2],
                                ButtonAnimation = this._buttonAnimation,
                                PrimaryText = item.description[0],
                                SecondaryText = item.description[1],
                                Value = Int32.Parse(Global.Settings.Get(item.name).ToString())
                            };
                            itmNumeric.OnValueChanged += delegate(object Sender, int Value) { Global.Settings.Set(item.name, itmNumeric.Value); };
                            this.listSettings.AddItem(itmNumeric);
                            break;
                        case Settings.MetaType.Text:
                            SensePanelTextboxItem itmText = new SensePanelTextboxItem()
                            {
                                Name = item.name,
                                LabelText = item.description[0],
                                Text = (string)Global.Settings.Get(item.name)
                            };
                            itmText.TextChanged += delegate(object Sender, EventArgs e) { Global.Settings.Set(item.name, itmText.Text); };
                            this.listSettings.AddItem(itmText);
                            break;
                    }
                }
            }
            listSettings.EndUpdate();
            this.Controls.Add(listSettings);
        }

        private void mitmSave_Click(object sender, EventArgs e)
        {
            Global.Settings.Save();
            this.Close();
        }

        private void mitmCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}